<?php

class STemplate {

   function STemplate() {
        global $Smarty;
        if (!isset($Smarty)) {
            $Smarty = new Smarty;
        }
    }

    function create() {
        global $Smarty;
        $Smarty = new Smarty();
        $Smarty->compile_check = true;
        $Smarty->debugging = false;
        $Smarty->template_dir = dirname(__FILE__) . "/../../templates";
        $Smarty->compile_dir  = dirname(__FILE__) . "/../../templates_c";
		
		// Load Javascript and CSS Cache System

		$Smarty->load_filter('output','join_javascript');
	//	$Smarty->load_filter('output','join_css');


        return true;
    }
    
    function setCompileDir($dir_name) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        $Smarty->compile_dir = $dir_name;
    }

    function setType($type) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        $Smarty->type = $type;
    }

  //=====================================================
  //  Assign Template Variable     
  //=====================================================

    function assign($var, $value) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        $Smarty->assign($var, $value);
    }
  
  //=====================================================
  //  Assign Template Variable (Array)          
  //=====================================================
  function tpl_array($input)
  {  
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }

	  foreach ($input as $key => $value)
      {
          $Smarty->assign($key, $value);
      }
  }
  //=====================================================
  //  Set Template Directory
  //=====================================================
    function setTplDir($dir_name = null) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        if (!$dir_name) {
            $Smarty->template_dir = dirname(__FILE__) . "/../../templates/default";
        } else {
            $Smarty->template_dir = $dir_name;
        }
    }

  //=====================================================
  //  Set Page Title             
  //=====================================================
  function set_title($title)
  { 
      $Smarty->assign("page_title", $title);
  }
  
  //=====================================================
  //  Set Meta Keywords           
  //=====================================================
  function set_meta_keywords($keywords)
  { 
      $Smarty->assign("meta_keywords", $keywords);
  }
  
  //=====================================================
  //  Set Meta Description          
  //=====================================================
  function set_meta_desc($desc)
  { 
      $Smarty->assign("meta_desc", $desc);
  }
  
  //=====================================================
  //  Set Page Tab           
  //=====================================================
  function set_tab($tab)
  { 
      $Smarty->assign("page_tab", $tab);
  }

    function setModule($module) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        $Smarty->theme = $module;
        $Smarty->type  = "module";
    }

    function setTheme($theme) {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        /*
        if (!file_exists(dirname(__FILE__) . "/../../templates/" . $theme)) {
            $theme = "default";
        } */
        $Smarty->template_dir = dirname(__FILE__) . "/../../templates/" . $theme;
        $Smarty->compile_dir  = dirname(__FILE__) . "/../../templates_c/" . $theme;
        $Smarty->theme        = $theme;
        $Smarty->type         = "theme";
    }

    function getTplDir() {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        return $Smarty->template_dir;
    }

    function display($filename) {
        global $Smarty;
	global $config;
        if (!isset($Smarty)) {
            STemplate::create();
        }
	if(strstr($filename,'admin/')){
	        $Smarty->display($filename);
	}else{
	        $Smarty->display('frontend/'.$config['theme'].'/'.$filename);
	}
    }

    function fetch($filename) {
        global $Smarty;
		global $config;
        if (!isset($Smarty)) {
            STemplate::create();
        }
		
		if(strstr($filename,'admin/')){
	       return $Smarty->fetch($filename);
		}else{
			 return	$Smarty->fetch('frontend/'.$config['theme'].'/'.$filename);
		}
		
    }
    
    function getVars() {
        global $Smarty;
        if (!isset($Smarty)) {
            STemplate::create();
        }
        return $Smarty->get_template_vars();
    }
}
?>